package com.d3works.studio.web;

import com.d3works.apps.base.db.App;
import com.d3works.apps.base.service.app.AppService;
import com.d3works.exception.D3worksException;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.d3works.studio.db.AppBuilder;
import com.d3works.studio.db.repo.AppBuilderRepository;
import java.io.IOException;

public class AppBuilderController {

  public void installApp(ActionRequest request, ActionResponse response)
      throws D3worksException, IOException {

    AppBuilder appBuilder = request.getContext().asType(AppBuilder.class);
    appBuilder = Beans.get(AppBuilderRepository.class).find(appBuilder.getId());

    App app = appBuilder.getGeneratedApp();
    Beans.get(AppService.class).installApp(app, null);

    response.setSignal("refresh-app", true);
  }

  public void uninstallApp(ActionRequest request, ActionResponse response) throws D3worksException {

    AppBuilder appBuilder = request.getContext().asType(AppBuilder.class);
    appBuilder = Beans.get(AppBuilderRepository.class).find(appBuilder.getId());

    App app = appBuilder.getGeneratedApp();
    Beans.get(AppService.class).unInstallApp(app);

    response.setSignal("refresh-app", true);
  }
}
